package com.ruoyi;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.time.LocalDate;

public class MySFTP {
    public static void main(String[] args) {
        LocalDate currentDate = LocalDate.now();
        int year = currentDate.getYear();
        int month = currentDate.getMonthValue();
        System.out.println("当前年份：" + year);
        System.out.println("当前月份：" + month);
        System.out.println(year+""+(month<10? "0":"") + month+"01");

        // 创建JSch对象
        JSch jsch = new JSch();

        // 设置Session参数
        String hostname = "192.168.101.130";
        int port = 22;
        String username = "king";
        String password = "123456";

        try {
            Session session = jsch.getSession(username, hostname, port);
            session.setPassword(password);

            // 设置参数，如需要进行主机密钥验证
            session.setConfig("StrictHostKeyChecking", "no");

            // 连接SFTP服务器
            session.connect();

            // 判断连接是否成功
            boolean isConnected = session.isConnected();
            System.out.println("hostname:"+hostname);
            System.out.println("port:"+port);
            System.out.println("username:"+username);
            System.out.println("password:"+password);
            if (isConnected) {
                System.out.println("SFTP连接成功！");
            } else {
                System.out.println("SFTP连接失败！");
            }

            // 断开连接
            session.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
